package com.api.shuigui.web;


import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson.JSONObject;
import com.api.shuigui.service.DocContentService;
import com.api.shuigui.service.ModelDataService;
import com.api.shuigui.service.impl.DocContentServiceImpl;
import com.api.shuigui.service.impl.ModelDataServiceImpl;
import com.engine.common.util.ParamUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/doc/getRoleTeam")
public class RoleTeamWeb extends HttpServlet {

    private static final Logger LOGGER = LoggerFactory.getLogger("RoleTeamWeb");

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        LOGGER.info(" /doc/getRoleTeam in function");
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/json;charset=utf-8");
        //跨域
        response.setHeader("Access-Control-Allow-Origin", "*");
        //初始化结果
        Map<String, Object> result = new HashMap<>();
        result.put("status", -1);
        PrintWriter out = null;
        try {
            out = response.getWriter();
            //读取流中的参数
            String paramJson = IoUtil.read(request.getInputStream(), "UTF-8");
            //初始化参数
            Map<String, Object> params;
            if ("".equals(paramJson)) {
                params = ParamUtil.request2Map(request);
            } else {
                params = JSONObject.parseObject(paramJson, Map.class);
            }
            //
            DocContentService doc = new DocContentServiceImpl();
            result.putAll(doc.getRoleTeam(params));
            result.put("status", 200);
            LOGGER.info(" result = {}", JSONObject.toJSONString(result));
            out.write(JSONObject.toJSONString(result));
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
            result.put("msg", "获取数据异常！");
            if (out != null) {
                out.write(JSONObject.toJSONString(result));
                out.flush();
            }
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        super.doPost(req, res);
    }
}
